﻿#ifndef CHATROOM_H
#define CHATROOM_H

#include <QWidget>
#include <QBasicTimer>
#include <QElapsedTimer>
#include <QMap>
#include <QSet>
#include "channelinfo.h"

namespace Ui {
class ChatRoom;
}

class QListWidgetItem;
class QMenu;
class NotifyLabel;
class MainWidget;

enum UserListRole
{
    ID_ROLE = Qt::UserRole,
    MEMEBR_ROLE,
    NAME_ROLE,
    MIC_ROLE,
    VIDEO_ROLE
};

class ChatRoom : public QWidget
{
    Q_OBJECT

public:
    explicit ChatRoom(const BaseChannelInfo &channel_info, QWidget *parent = Q_NULLPTR);
    ~ChatRoom();
    qint64 getUniqueId() const;
    qint64 getId() const;
    qint64 getRid() const;
    void initWith(MainWidget* main);
    bool hasFunction(int functionId);
    void clearMessage();
    virtual bool handle_video_contentMenu(QMenu *menu, int micid, qint64 uid);
    virtual void handle_share_command();

    void handle_mic_state(const QList<QVariantHash> &mics, const QList<QVariantHash> &applies, bool isInit = false);
    void handle_text_msg(int type, const QVariantHash &msg);
    virtual void handle_msg(int type, const QVariantHash &msg) = 0;
    void handle_msg_list(const QList<QVariantHash> &msgList);

    void handle_notify(qint64 src_id, const std::string &text, const std::string &name);

    void handle_local_video(qint64 user_id, const QPixmap &pix);
    void handle_remote_video(qint64 user_id, const QPixmap &pix);
    void handle_audio_volume(qint64 user_id, int volume);
protected:
    void keyPressEvent(QKeyEvent *e);
    void keyReleaseEvent(QKeyEvent *e);
    void timerEvent(QTimerEvent *e);
    virtual QByteArray send_msg(int msg_type, const QString& text) = 0;
private slots:
    void slot_textShow_cleared();

    void on_chatWidget_sendClicked();
    void on_chatWidget_sendImage(const QPixmap &pixmap);

    void on_button_tospeak_pressed();

    void on_button_tospeak_released();

    void on_button_device_clicked();

    void on_button_micphone_toggled(bool checked);

    void on_button_speaker_toggled(bool checked);

    void on_button_screen_clicked(bool checked);

#ifdef Q_OS_WIN
    void on_button_window_clicked(bool checked);
#endif

    virtual void on_button_meeting_clicked(bool checked) = 0;

    virtual void on_button_camera_clicked(bool checked) = 0;

    virtual void on_list_online_user_customContextMenuRequested(const QPoint &pos) = 0;
signals:
    void deleteChannelMessage(qint64 uniqueId);
protected:
    int getMicId(qint64 user_id);
    QString getName(qint64 user_id);
    QString getName(qint64 user_id, const QString &defaultName);
    QString getName(qint64 user_id, const std::string &name);
    QPixmap getDecoration(qint64 user_id) const;
    virtual void startSpeak();
    virtual void stopSpeak() = 0;
    void setVideoEffect(bool on, bool useLock);
    void startNotifySpeak(qint64 user_id, bool force);
    void stopNotifySpeak(qint64 user_id);
    void showNotifyText(const QString &text);
    void refreshUserListViews(const QList<QVariantHash> &member_list) ;
    void setButtonStatus(qint8 camera, qint8 meeting, qint8 tospeak);

    Ui::ChatRoom *ui;
    const qint64 uniqueId;
    const qint64 myid;

    QVector<QVariantHash> mic_list_;
    QVector<qint64> apply_list_;

    QSet<qint64> online_list;
    QMap<qint64, QListWidgetItem*> userListItems;
    bool microphone_mute_inited;
    bool self_speaking;
    QWidget* acting_button;
    int volumeShowing;
    int volumeCalculated;
    QBasicTimer volumeTimer;
    QSet<int> function_list;
    QMap<qint64, QElapsedTimer> notifySpeakCheck;
    QBasicTimer notifySpeakTimer;
    NotifyLabel *notifyLabel;
};

#endif // CHATROOM_H
